export function getFirstAndLastDayOfMonth(target) {
  // 获取当前日期
  const today = new Date();
  // 获取当前月的第一天
  const firstDayOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
  // 获取下个月的第一天
  const firstDayOfNextMonth = new Date(today.getFullYear(), today.getMonth() + 1, 1);
  // 获取当前月的最后一天
  const lastDayOfMonth = new Date(firstDayOfNextMonth - 1);
  // 格式化日期为 YYYY-MM-DD
  const formatDate = date => {
    const year = date.getFullYear();
    const month = (date.getMonth() + 1).toString().padStart(2, "0");
    const day = date.getDate().toString().padStart(2, "0");
    const hours = date.getHours().toString().padStart(2, "0");
    const minutes = date.getMinutes().toString().padStart(2, "0");
    const seconds = date.getSeconds().toString().padStart(2, "0");
    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  };

  // 返回格式化的日期
  return {
    firstDay: formatDate(firstDayOfMonth),
    lastDay: formatDate(lastDayOfMonth)
  };
}

function parseMonthYear(str) {
  const [year, month] = str.split("-").map(num => parseInt(num, 10));
  return { year, month };
}

export function getFirstAndLastDayOfMonthTarget(year, month) {
  const firstDay = new Date(year, month - 1, 1); // JavaScript 中月份是从 0 开始的
  const lastDay = new Date(year, month, 0); // 获取下个月的第一天，然后减去一天

  // 格式化日期为 yyyy-mm-dd HH:mm:ss
  const formatDate = (date, isLastDay) => {
    const year = date.getFullYear();
    const month = (date.getMonth() + 1).toString().padStart(2, "0");
    const day = date.getDate().toString().padStart(2, "0");
    const hours = isLastDay ? "23" : date.getHours().toString().padStart(2, "0");
    const minutes = isLastDay ? "59" : date.getMinutes().toString().padStart(2, "0");
    const seconds = isLastDay ? "59" : date.getSeconds().toString().padStart(2, "0");
    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  };

  return {
    startTime: formatDate(firstDay, false),
    endTime: formatDate(lastDay, true)
  };
}

export function convertDateFormat(dateString) {
  const date = new Date(dateString);

  // 获取年、月、日
  const year = date.getFullYear();
  const month = date.getMonth() + 1; // getMonth() 返回的月份是从 0 开始的
  const day = date.getDate();

  // 构建新的日期字符串
  return `${year}-${month}-${day}`;
}

export const formatData = (arr, key1 = "name", key2 = "code") => {
  const newArr = arr.map(v => {
    return {
      ...v,
      text: v[key1],
      value: v[key2]
    };
  });
  return newArr;
};

export function formatDateTime(dateString) {
  const [year, month, day] = dateString.split("-").map(part => parseInt(part, 10));
  return `${year}-${String(month).padStart(2, "0")}-${String(day).padStart(2, "0")}`;
}


